//
//  CYCSDetailVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/9/27.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCSDetailVC.h"
#import "CYCSRecordCell.h"
#import "CYCSRecordModel.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "CYHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYCSMoreVC.h"
#import "MBProgressHUD+LE.h"
#import "UIAlertView+Block.h"
#import "UIImageView+WebCache.h"
#import "UIAlertView+Block.h"
#import "DCPaymentView.h"
#import "CYDES3Util.h"


@interface CYCSDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
     NSMutableArray *recordArr;//数据源
     
}
@property (weak, nonatomic) IBOutlet UIView *loadingView;
@property (weak, nonatomic) IBOutlet UIImageView *loadingImageView;
@property (weak, nonatomic) IBOutlet UILabel *endLab;


@property (weak, nonatomic) IBOutlet UIImageView *iamgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *idcardLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UIView *fengeLine;

@property (nonatomic, strong) UITableView *messageTabelView;
@property (weak, nonatomic) IBOutlet UIView *rongqiView;
@property (nonatomic, strong) UILabel *noticeLab;

@property (nonatomic, strong) NSString *statueStr;

@property (weak, nonatomic) IBOutlet UIButton *xiajiaBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (weak, nonatomic) IBOutlet UIImageView *statuesImageView;




@end

@implementation CYCSDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
     
     recordArr = [[NSMutableArray alloc]init];
     
      [self.iamgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.credit-manage.com/%@",_cycuishoumodel.evidencePhotos]]];
     
     if (_cycuishoumodel.statusId.intValue == 1) {
          //已完成
          [_statuesImageView setImage:[UIImage imageNamed:@"3"]];
          
     }
     
     _nameLab.text = [NSString stringWithFormat:@"违约人：%@", _cycuishoumodel.name];
     _idcardLab.text = _cycuishoumodel.certificateNo;
     _phoneLab.text = _cycuishoumodel.phone;
     if (_cycuishoumodel.statusId.intValue == 2) {
          [_statuesImageView setImage:[UIImage imageNamed:@"3"]];
          //可删除
          _xiajiaBtn.hidden = YES;
          _deleteBtn.hidden = NO;
     }
     
     [self createRecordTableView];
     [self loaddata];
     
    // Do any additional setup after loading the view from its nib.
}

-(void)loaddata{
     [recordArr removeAllObjects];
     [MBProgressHUD showMessage:nil toView:self.view];
     CYUser *user = [CYLoginHttpTool userData].user;
     NSString *defaultId = _cycuishoumodel.defaulterId;
     CYLog(@"defaultId = %@",defaultId);
     
     [CYHttpTool postWithURL:[CYURL shared].getAppDefaulterPayBack params:@{@"userId":user.userId,@"token":user.token,@"defaulterId":defaultId} success:^(id json) {
          [MBProgressHUD hideHUDForView:self.view];
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          if (jsoninfo.isSuccess) {
               NSArray *arr = jsoninfo.data;
               if (arr.count) {
                    for (NSDictionary *dic in arr) {
                         CYCuiShouModel *temp = [[CYCuiShouModel alloc]initWithDictionary:dic];
                         [recordArr addObject:temp];
                         
                    }
               }else if(!arr.count){
                    _noticeLab.hidden = NO;
                    
               }
               
            [_messageTabelView reloadData];
               
               
          }else{
               CYAlertView(jsoninfo.message);
               [MBProgressHUD hideHUDForView:self.view];
               
          }
          
          
     } failure:^(NSError *error) {
          [MBProgressHUD hideHUDForView:self.view];
          
     }];
     
}

-(void)createRecordTableView{

     _messageTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0,_fengeLine.y, [[UIScreen mainScreen] bounds].size.width, self.rongqiView.height-_fengeLine.y-10) style:UITableViewStylePlain];
     _messageTabelView.backgroundColor = [UIColor whiteColor];
     _messageTabelView.showsHorizontalScrollIndicator=NO;
     _messageTabelView.showsVerticalScrollIndicator=NO;
     self.messageTabelView.dataSource = self;
     self.messageTabelView.delegate = self;
     //    [_messageTabelView registerNib:[UINib nibWithNibName:@"CYPersonReCell" bundle:nil] forCellReuseIdentifier:@"CYPersonReCell"];
     [_messageTabelView registerNib:[UINib nibWithNibName:@"CYCSRecordCell" bundle:nil] forCellReuseIdentifier:@"CYCSRecordCell"];
//     [CYTool setExtraCellLineHiddenWithTableView:_messageTabelView];
     [self.rongqiView addSubview:_messageTabelView];
     
     _noticeLab = [[UILabel alloc]initWithFrame:CGRectMake(0,_fengeLine.y, [[UIScreen mainScreen] bounds].size.width, self.rongqiView.height-_fengeLine.y-10)];
     _noticeLab.text = @"暂无新的催收记录";
     _noticeLab.textAlignment = NSTextAlignmentCenter;
     _noticeLab.hidden = YES;
     _noticeLab.backgroundColor = [UIColor whiteColor];
     [self.rongqiView addSubview:_noticeLab];

     
     
     
     
}

#pragma mark - tableview代理
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     
      CYCSRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYCSRecordCell" forIndexPath:indexPath];
     
     //    CYPersonReCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYPersonReCell" forIndexPath:indexPath];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     cell.model = recordArr[indexPath.row];
     
     
     return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return 40;
     
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
     
//           CYCSDetailVC *detail = [[CYCSDetailVC alloc]init];
//          detail.cycuishoumodel = [_cuishoudataArray objectAtIndex:indexPath.row];
//          [self.navigationController pushViewController:detail animated:YES];
     
          
          
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
         return recordArr.count;
}
#pragma mark -
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
     self= [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
          
     }
     return self;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
     
     
    // Dispose of any resources that can be recreated.
}


- (IBAction)moreClick:(id)sender {

     CYCSMoreVC *more = [[CYCSMoreVC alloc]init];
     more.cycuishoumodel = _cycuishoumodel;
     [self.navigationController pushViewController:more animated:YES];
     
}

- (IBAction)setdownClick:(id)sender {
    
     CYUser *user = [CYLoginHttpTool userData].user;
     
     DCPaymentView *payAlert = [[DCPaymentView alloc]init];
     payAlert.detail = @"请输入交易密码";
     [payAlert show];
     payAlert.completeHandle = ^(NSString *inputPwd) {
           NSString *desstr = [CYDES3Util encrypt:inputPwd];
          [MBProgressHUD showMessage:nil toView:self.view];
          [CYHttpTool postWithURL:[CYURL shared].outAppDefaultter params:@{@"userId":user.userId,@"token":user.token,@"defaulterId":_cycuishoumodel.defaulterId,@"dealPassword":desstr} success:^(id json) {
               
               [MBProgressHUD hideHUDForView:self.view];
               CYJsonInfo *jsoninfo1 = [CYJsonInfo objectWithKeyValues:json];
               CYLog(@"jsoninfo = %@",jsoninfo1);
               NSString *yongjinstr = jsoninfo1.data;
               if (yongjinstr && yongjinstr.intValue == 1) {
                    //下架成功
                    [self.navigationController popViewControllerAnimated:YES];
                    CYAlertView(jsoninfo1.message);
                    [MBProgressHUD hideHUDForView:self.view];
                    
               }else{
                    CYAlertView(jsoninfo1.message)
                    return ;
               }
          } failure:^(NSError *error) {
               CYLog(@"erro = %@",error);
               [MBProgressHUD hideHUDForView:self.view];
               
          }];
          
          
     };
     
    
           
     
     
}
- (IBAction)deleteClick:(id)sender {
     
     CYUser *user = [CYLoginHttpTool userData].user;
     [MBProgressHUD showMessage:nil toView:self.view];
     [CYHttpTool postWithURL:[CYURL shared].updateAppDisplayId params:@{@"userId":user.userId,@"token":user.token,@"defaulterId":_cycuishoumodel.defaulterId} success:^(id json) {
          [MBProgressHUD hideHUDForView:self.view];
          CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
          CYAlertView(jsoninfo.message);

          if (jsoninfo.isSuccess) {
               [self.navigationController popViewControllerAnimated:YES];
          }
         
     } failure:^(NSError *error) {
          CYLog(@"erro = %@",error);
          [MBProgressHUD hideHUDForView:self.view];
          
     }];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
